'use strict';

const { Controller } = require('egg');
const { Op } = require('sequelize');
const dayjs = require("dayjs");
const serviceName = 'cycle'
class RouterController extends Controller {
    // 分页
    async index() {
        const { query, service } = this.ctx;
        let { pageNum, pageSize, ship_id = '', start_time, end_time  } = query;
        let page = Number(pageNum) || 1;
        let size = Number(pageSize) || 10;
        const where = {

        };
        if (ship_id) {
            where.ship_id = ship_id
        }
        if (start_time) {
            where.start_time = {
                [Op.gte]: dayjs(start_time).format('YYYY-MM-DD') + ' 00:00:00',
            }
        }
         if (end_time) {
            where.start_time = {
                [Op.lte]: dayjs(start_time).format('YYYY-MM-DD') + ' 23:59:59'
            }
         }
         if (start_time && end_time) {
            where.start_time = {
                [Op.gte]: dayjs(start_time).format('YYYY-MM-DD') + ' 00:00:00',
                [Op.lte]: dayjs(start_time).format('YYYY-MM-DD') + ' 23:59:59'
            }

         }

        const list = await service[serviceName].getPage(where, page, size);
        this.ctx.success(list);
    }
    // 新增
    async batchCreate() {
        const { ctx } = this;
        const data = ctx.request.body;
        const res = await ctx.service[serviceName].batchCreate(data);
        ctx.success(res, 200);
    }
    async create() {
        const { ctx } = this;
        const data = ctx.request.body;
        const res = await ctx.service[serviceName].create(data);
        ctx.success(res, 200);
    }

    async createCustom() {
        const { ctx } = this;
        const data = ctx.request.body;
        const res = await ctx.service[serviceName].createCustom(data);
        ctx.success(res, 200);
    }
    // 修改
    async update() {
        const { ctx } = this;
        const id = ctx.params.id;
        const infos = ctx.request.body;
        if (!id) throw new Error('必须传递ID参数');
        if (Object.keys(infos).length === 0) throw new Error('请传递修改的内容');
        await ctx.service[serviceName].update(id, infos);
        ctx.success(id, 200);
    }
    // 删除
    async destroy() {
        const uid = Number(this.ctx.params.id);
        if (!uid) throw new Error('ID 有误');
        await this.ctx.service[serviceName].deleteById(uid);
        this.ctx.success(uid); // 删除成功返回被删除的用户ID
    }
    // 查看
    async show() {
        const { params, service } = this.ctx;
        const findItem = await service[serviceName].findOne(params.id);
        this.ctx.success(findItem);
    }
    // 所有列表
    async allList() {
        const { service } = this.ctx;
        const { ship_id, start_time,afterDate } = this.ctx.query;
        let where = {

        }
        console.log('ship_id',ship_id)
        if (ship_id) {
            where.ship_id = ship_id
        }
        if (start_time) {
            // 查询当天的船票
            where.start_time = {
                [Op.gte]: start_time + ' 00:00:00',
                [Op.lte]: start_time + ' 23:59:59'
            }
        }
        if (afterDate) {
            // 查询大于afterDate次日日期的数据
            // where.start_time > dayjs(afterDate).add(1, 'day').format('YYYY-MM-DD 00:00:00')
            // where.start_time = {
            //     [Op.gte]: dayjs(afterDate).format('YYYY-MM-DD 00:00:00')
            // }
        }
        const list = await service[serviceName].allList(where);
        this.ctx.success(list);
    }
}

module.exports = RouterController;
